library(fixest)
library(readr)
library(broom.mixed)
library(texreg)
library(dplyr)
library(fixest)
library(patchwork)
library(ggplot2)

# Cross sectional linear FE models that use aggregate data (2000-2019), here robustness test where only directional demands
# i.e. centripetal = going towards center =  gov. inlcusion and no demands vs. centrifugal = away = reg. autonomy or secession
# are taken into account for agreement measures

source("funs_and_constants.R")

# load in data
epr_yearly_group_clusters_h1 <- read_csv("data/epro_data_h1.csv")
epro_year_group_clusters <- read_csv("data/epro_data_h2.csv")
epro_year_group_clusters_geo <- read_csv("data/epro_geo_data_h3.csv")

mean_h1 <- epr_yearly_group_clusters_h1 %>% 
  filter(year >= 2000) %>%# only obs after 2000 because of EPR 2.0 coding
  group_by(gwgroupid, countries_gwid) %>%
  summarize(
    across(c(directional_agreement, n_orgs, resource_and_agriculture, regaut, groupsize, incidence_flag, status_pwrrank, any_multiethnic, nightlight_total_pc_log, n_tek_groups),
           ~ mean(.x, na.rm = T)),
    none_one_or_both_nats_agri_char = Mode(as.factor(none_one_or_both_nats_agri_char), na.rm = TRUE)
  )

mean_h2 <- epro_year_group_clusters %>% 
  filter(year >= 2000) %>%# only obs after 2000 because of EPR 2.0 coding
  group_by(gwgroupid, countries_gwid) %>%
  summarize(
    across(c(directional_agreement, n_orgs, n_ed_religions, hhi_rel, religious_segments_bin, regaut, groupsize, incidence_flag, status_pwrrank, any_multiethnic, nightlight_total_pc_log, n_tek_groups),
           ~ mean(.x, na.rm = T)))

mean_h3 <- epro_year_group_clusters_geo %>% 
  filter(year >= 2000) %>%# only obs after 2000 because of EPR 2.0 coding
  group_by(gwgroupid, countries_gwid) %>%
  summarize(
    across(c(directional_agreement, n_non_intersection_group_polygons, multiple_polygons_bin, spatial_hhi, n_orgs, regaut, groupsize, incidence_flag, status_pwrrank, any_multiethnic, nightlight_total_pc_log, n_tek_groups),
           ~ mean(.x, na.rm = T)))


#######
## H1
######

full_model_h1_max <- feols(directional_agreement ~ resource_and_agriculture + n_orgs + regaut + groupsize + incidence_flag + status_pwrrank + any_multiethnic + nightlight_total_pc_log + n_tek_groups | countries_gwid,
                           cluster = ~countries_gwid, data = mean_h1)

full_model_h1_n_resources_max <- feols(directional_agreement ~ none_one_or_both_nats_agri_char + n_orgs + regaut + groupsize + incidence_flag + status_pwrrank + any_multiethnic + nightlight_total_pc_log + n_tek_groups |
                                         countries_gwid,
                                       cluster = ~countries_gwid, data = mean_h1)

screenreg(list(full_model_h1_max, full_model_h1_n_resources_max),
          stars = stars)


######
### H2
#######


full_model_h2_max <- feols(directional_agreement ~ religious_segments_bin + n_orgs + regaut + groupsize + incidence_flag + status_pwrrank + any_multiethnic + nightlight_total_pc_log + n_tek_groups | countries_gwid,
                           cluster = ~countries_gwid, data = mean_h2)

full_model_h2_max_n_rels <- feols(directional_agreement ~ n_ed_religions + n_orgs + regaut + groupsize + incidence_flag + status_pwrrank + any_multiethnic + nightlight_total_pc_log + n_tek_groups | countries_gwid,
                                  cluster = ~countries_gwid, data = mean_h2)

full_model_h2_max_herf <- feols(directional_agreement ~ hhi_rel + n_orgs + regaut + groupsize + incidence_flag + status_pwrrank + any_multiethnic + nightlight_total_pc_log + n_tek_groups | countries_gwid,
                                cluster = ~countries_gwid, data = mean_h2)

screenreg(list(full_model_h2_max, full_model_h2_max_n_rels, full_model_h2_max_herf),
          stars = stars)



########
#### H3
########

full_model_h3_max <- feols(directional_agreement ~ n_non_intersection_group_polygons + n_orgs + regaut + groupsize + incidence_flag + status_pwrrank + any_multiethnic + nightlight_total_pc_log + n_tek_groups | countries_gwid,
                           cluster = ~countries_gwid, data = mean_h3)

full_model_h3_max_bin <- feols(directional_agreement ~ multiple_polygons_bin + n_orgs + regaut + groupsize + incidence_flag + status_pwrrank + any_multiethnic + nightlight_total_pc_log + n_tek_groups | countries_gwid,
                               cluster = ~countries_gwid, data = mean_h3)

full_model_h3_max_hhi <- feols(directional_agreement ~ spatial_hhi + n_orgs + regaut + groupsize + incidence_flag + status_pwrrank + any_multiethnic + nightlight_total_pc_log + n_tek_groups | countries_gwid,
                               cluster = ~countries_gwid, data = mean_h3)


screenreg(list(full_model_h3_max, full_model_h3_max_hhi, full_model_h3_max_bin),
          stars = stars)


# table for appendix
texreg(list(full_model_h1_max, full_model_h2_max, full_model_h3_max),
       stars = stars,
       custom.coef.map = coef_map,
       custom.header = list("DV: Directional Disagreement (Centrifugal vs. Centripetal)" = 1:3),
       table = FALSE,
       include.proj.stats = FALSE,
       file = "tables/centrifugal_centripetal.tex")

